package com.winit.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.core.server.RedisServer;
import com.winit.core.server.base.CmdOutputHandler;
import com.winit.core.server.factory.LinuxJumpServerFactory;
import com.winit.deploy.server.WinitJumpServer;
import com.winit.jumpServer.bean.JumpServerInfo;
import com.winit.jumpServer.util.JumpServerListInfo;
import com.winit.uCloud.bean.UCloudCRedisInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 清理redis缓存
 */
public class RedisUtil {
    static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);

    public static void cleanProjectRedis(String projectName, CmdOutputHandler handler) throws Exception {
        JSONObject object = JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "data/project_redis.json", JSONObject.class);
        if (!object.containsKey(projectName)) {
            logger.info("com.winit.common.util.RedisUtil.cleanProjectRedis 工程" + projectName + "不需要清理缓存");
            return;
        }
        List<UCloudCRedisInfo> list = JSONArray.parseArray(object.getString(projectName), UCloudCRedisInfo.class);
        if (list.size() == 0) {
            logger.info("com.winit.common.util.RedisUtil.cleanProjectRedis 工程" + projectName + "配置的redis地址为空");
            return;
        }
        for (UCloudCRedisInfo info : list) {
            cleanRedis(info.getJumpServer(), info.getIp(), info.getSshPort(), info.getRedisPort(), handler);
        }
    }


    public static void cleanRedis(String jumpServerIp, String redisIp, Integer sshPort, List<Integer> redisPorts) throws Exception {
        CmdOutputHandler handler = new CmdOutputHandler() {
            @Override
            public void onMessage(String cmd, int index, String message, Boolean isErrorMessage) {
            }
        };
        cleanRedis(jumpServerIp, redisIp, sshPort, redisPorts, handler);
    }

    public static void cleanRedis(String jumpServerIp, String redisIp, Integer sshPort, List<Integer> redisPorts, CmdOutputHandler handler) throws Exception {
        WinitJumpServer jumpServer = null;
        if (!StringUtils.isEmpty(jumpServerIp)) {
            jumpServer = LinuxJumpServerFactory.getInstance().getJumpServer(jumpServerIp);
            if (jumpServer == null) {
                throw new Exception("清空c环境redis缓存,获取跳板机对象为空.jump:" + jumpServerIp
                        + "redis:" + redisIp + ",请联系beetle管理员!");
            }
        }
        RedisServer redisServer = new RedisServer(redisIp, "beetle", sshPort);
        redisServer.addPrivateKeyPath("/home/" + ConfigUtil.getValue("ssh_login_user") + "/" + ConfigUtil.getValue("ssh_key_file"));
        redisServer.setConnectionTimeout(Constants.SSH_CONNECT_TIMEOUT);
        if (jumpServer != null) {
            jumpServer.jump(redisServer);
        }
        redisServer.connect();
        redisServer.doClean(redisPorts, handler);
        redisServer.disConnect();
    }
}
